package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.*;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.StuAnswerMapper;
import com.example.demo.mapper.UpdateLogMapper;
import com.example.demo.service.StuAnswerService;
import com.example.demo.service.UpdateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UpdateLogServiceImpl extends ServiceImpl<UpdateLogMapper, UpdateLog> implements UpdateLogService {

    @Autowired
    private UpdateLogMapper updateLogMapper;
    @Autowired
    private StuAnswerMapper stuAnswerMapper;
    @Autowired
    private QuestionMapper questionMapper;

//    @Override
  /*增加修改成绩日志记录*/
    public Map<String,Object> addupdatelog(String studentName, String studentId, String stuclass, int exerciseId, int questionId,String answer,String stuanswer,String stuidea,int oldscore,int newscore, String difficulty, String opid, String opname, Date optime){
        QueryWrapper<StuAnswer> stuAnswerQueryWrapper=new QueryWrapper<>();
        stuAnswerQueryWrapper.eq("studentId",studentId).eq("exerciseId",exerciseId).eq("questionId",questionId);
        StuAnswer stuAnswer=stuAnswerMapper.selectOne(stuAnswerQueryWrapper);
        System.out.println(11111);
        System.out.println(studentId+exerciseId+questionId);
        System.out.println(11111);
        Map<String,Object> modelMap = new HashMap<String, Object>();
        if(stuAnswer==null){
            modelMap.put("state","error:not exists");
            modelMap.put("errmessage","未作答不能修改");
            return modelMap;
        }
        List<SQLResult> results = questionMapper.getTeacherQuestion(questionId);
        SQLResult sqlResult = results.get(0);

        UpdateLog updatalog = new UpdateLog();
        updatalog.setStudentName(studentName);
        updatalog.setStudentId(studentId);
        updatalog.setStuclass(stuclass);
        updatalog.setExerciseId(exerciseId);
        updatalog.setQuestionId(questionId);
        updatalog.setAnswer(answer);
        updatalog.setStuanswer(stuanswer);
        updatalog.setStuidea(stuidea);
        updatalog.setOldscore(oldscore);
        updatalog.setNewscore(newscore);
        updatalog.setDifficulty(difficulty);
        updatalog.setOpid(opid);
        updatalog.setOpname(opname);
        updatalog.setOptime(optime);
        updatalog.setQcontent(sqlResult.getQuestion().getContent());
        System.out.println(updatalog.getStudentName());
        int mes = updateLogMapper.insert(updatalog);
        if(mes==1){
            modelMap.put("state","success");
        }
        else {
            modelMap.put("state","插入日志记录失败");
        }

        return modelMap;
    }

    //修改日志记录
    public Map<String,Object> modifyupdatelog(int id,String studentName, String studentId, String stuclass, int exerciseId, int questionId,String qtitle, String qcontent, String difficulty, String opid, String opname, Date optime){
        QueryWrapper<UpdateLog> UpdateLogQueryWrapper=new QueryWrapper<>();
        UpdateLogQueryWrapper.eq("id",id);
        UpdateLog updateLog=updateLogMapper.selectOne(UpdateLogQueryWrapper);

        Map<String,Object> modelMap = new HashMap<String, Object>();
        if(updateLog==null){
            modelMap.put("state","error:not exists");
            modelMap.put("errmessage","不存在该日志记录");
            return modelMap;
        }
        else{
            updateLog.setStudentName(studentName);
            updateLog.setStudentId(studentId);
            updateLog.setStuclass(stuclass);
            updateLog.setExerciseId(exerciseId);
            updateLog.setQuestionId(questionId);
            updateLog.setQcontent(qcontent);
            updateLog.setDifficulty(difficulty);
            updateLog.setOpid(opid);
            updateLog.setOpname(opname);
            updateLog.setOptime(optime);
            UpdateWrapper<UpdateLog> logUpdateWrapper=new UpdateWrapper<>();
            logUpdateWrapper.eq("id",id);
            int mes=updateLogMapper.update(updateLog,logUpdateWrapper);
            if(mes==1){
                modelMap.put("state","success");
            }
            else {
                modelMap.put("state","error:update failed");
            }
        }
        return modelMap;
    }

    //查询所有的修改日志
    public List<Map<String,Object>> getUpdateLog(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        List<UpdateLog> updateLogs=updateLogMapper.getUpdateLog();
        List<Map<String,Object>> result=new ArrayList<Map<String, Object>>();
        for(UpdateLog s:updateLogs){
            Map<String,Object> modelMap = new HashMap<String, Object>();
            modelMap.put("id",s.getId());
            modelMap.put("studentname",s.getStudentName());
            modelMap.put("studentid",s.getStudentId());
            modelMap.put("stuclass",s.getStuclass());
            modelMap.put("exerciseid",s.getExerciseId());
            modelMap.put("questionid",s.getQuestionId());
            modelMap.put("qcontent",s.getQcontent());

            modelMap.put("answer",s.getAnswer());
            modelMap.put("stuanswer",s.getStuanswer());
            modelMap.put("stuidea",s.getStuidea());
            modelMap.put("oldscore",s.getOldscore());
            modelMap.put("newscore",s.getNewscore());

            modelMap.put("difficulty",s.getDifficulty());
            modelMap.put("opid",s.getOpid());
            modelMap.put("opname",s.getOpname());
            modelMap.put("optime",sdf.format(s.getOptime()));
            result.add(modelMap);
        }
        return result;
    }

    //删除日志
    public Map<String,Object> deletelog(int id){
        Map<String, Object> modelMap = new HashMap<>();

        QueryWrapper<UpdateLog> logWrapper = new QueryWrapper<>();
        logWrapper.eq("id", id);
        UpdateLog updateLog = updateLogMapper.selectOne(logWrapper);
        if (updateLog == null) {
            modelMap.put("state", "error");
//    		modelMap.put("detail", "student doesn't exist");
            modelMap.put("detail", "日志不存在");
            return modelMap;
        }

        int mes = updateLogMapper.delete(logWrapper);
        if (mes == 1) {
            modelMap.put("state", "success");
        } else {
            modelMap.put("state", "error");
        }
        return modelMap;
    }
}
